import Vue from 'vue'
import VueRouter from 'vue-router'
import modules from './module';

Vue.use(VueRouter)

const Index = () => import('@/views/index');
const Login = () => import('@/views/user/login')

const routes = [
  {
    path: '/',
    alias:'/index',
    name: '首页',
    component: Index,
    meta:{
      requireAuth:true,
      isShowNav:true
    },
    children:modules
  },
  {
    path:'/login',
    name:'登录',
    component:Login,
    meta:{
      isShowNav:false
    }
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫，根据登录状态拦截路由
router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('token');
  if(to.meta.requireAuth){
    if(token){
      next();
    }else {
      Vue.prototype.$msg({
        message:'请登录',
        type:'warning'
      })
      next('/login');
    }
  }else {
    next();
  }
})

export default router
